package com.lovo.invoicing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.ProviderBean;
import com.lovo.invoicing.service.IProviderService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/provider")
public class ProviderController {
    @Autowired
    IProviderService providerService;
    /**
     * logger
     */
    private Logger logger = LoggerFactory.getLogger(ProviderController.class);



    @RequestMapping("/addProvider")
    @Transactional
    public String addProvider(@RequestBody ProviderBean providerBean){
        providerService.addProvider(providerBean);
        return "success";
    }

    @RequestMapping("/findProviderById")
    public ProviderBean findProviderById(int providerId){
       return providerService.queryProviderById(providerId);
    }

    @RequestMapping("/modifyProvider")
    public String modifyProvider(@RequestBody ProviderBean providerBean){
        providerService.updateProvider(providerBean);
        return "success";
    }

    @RequestMapping("/findProvidersByPage")
    public IPage<ProviderBean> findProvidersByPage(@RequestBody PageBean<ProviderBean> pageBean){
        return providerService.queryProvidersByPage(pageBean);
    }

    @RequestMapping("/findAllProviders")
    public List<ProviderBean> findAllProviderName(){
        return providerService.queryAllProviders();
    }

    @RequestMapping("/importExcel")
    public Map<String, Object> importExcel(@RequestParam("file") MultipartFile file){
        Map<String, Object> map = new HashMap<>();
        try {
            map = providerService.importExcel(file);
        }catch (Exception e){
            map.put("status",-1);
            map.put("data", "导入异常");
            logger.error(e.getMessage(),e);
        }
        return map;
    }

}
